<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-box">
      <my-search @click="goSearch"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-active-color="'#999'" :indicator-color="'#fff'" :indicator-dots="true" :autoplay="true"
      :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in swiperList" :key="index">
        <navigator :url="'/subpkg/goods_detail/goods_detail?goods_detail=' + item.goods_id" class="swiper-item">
          <image :src="item.image_src" alt="">
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航栏 -->
    <view class="navigator">
      <view @click="navClick(item)" v-for="(item,index) in categoriesList" :key="index" class="nav-item">
        <image :src="item.image_src">
      </view>
    </view>
    <!-- 楼层 -->
    <view class="foor-list">
      <view class="item" v-for="(item, index) in floorList" :key="index">
        <image :src="item.floor_title.image_src" mode="">
          <!-- 楼层内结构 -->
          <view class="floor-detail">
            <view class="left">
              <image @click="floorClick(item.product_list[0].url)" :src="item.product_list[0].image_src" mode="widthFix"
                :style="{width: item.product_list[0].image_width + 'rpx'}">
            </view>
            <view class="right">
              <image v-for="(item2, index2) in item.product_list" :key="index2" :src="item2.image_src"
                :style="{width: item2.image_width + 'rpx'}" mode="widthFix" v-if="index2 !== 0"
                @click="floorClick(item.product_list[index2].url)">
            </view>
          </view>
      </view>
    </view>
  </view>

</template>

<script>
  import setBadge from '@/mixin/tabBarBadge.js'
  export default {
    mixins: [setBadge],
    data() {
      return {
        // 这是轮播图数据列表
        swiperList: [],
        categoriesList: [],
        floorList: []
      };
    },
    onLoad() {
      // 调用方法获取轮播图的数据
      this.getSwiperList()
      // 调用方法获得分类页数据
      this.getCategoriesList()
      // 调用方法获取楼层数据
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const res = await uni.$http.get('api/public/v1/home/swiperdata')
        if (res.data.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.swiperList = res.data.message
      },

      async getCategoriesList() {
        const {
          data: res,
          statusCode
        } = await uni.$http.get('api/public/v1/home/catitems')
        if (statusCode !== 200) {
          return uni.$showMsg()
        }
        this.categoriesList = res.message
      },

      // 导航栏被点击时触发的事件
      navClick(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '../cate/cate'
          })
        }
      },

      // 获取楼层数据
      async getFloorList() {
        const {
          data,
          statusCode
        } = await uni.$http.get('api/public/v1/home/floordata')
        if (statusCode !== 200) {
          return uni.$showMsg()
        }
        data.message.forEach(item => {
          item.product_list.forEach(item2 => {
            item2.url = '/subpkg/goods_list/goods_list?' + item2.navigator_url.split('?')[1]
          })
        })
        this.floorList = data.message
      },

      //楼层点击时执行 
      floorClick(url) {
        uni.navigateTo({
          url
        })
      },

      // 跳转到搜索页
      goSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  // 分类导航
  .navigator {
    margin: 15rpx 0;
    display: flex;
    justify-content: space-around;

    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  // 楼层
  .foor-list {
    .item {
      image {
        width: 100%;
        height: 60rpx;
      }
    }

    // 楼层详情结构
    .floor-detail {
      overflow: hidden;
      display: flex;

      .left {
        padding-left: 10rpx;
      }

      .right {
        margin: 0 10rpx;
        display: flex;
        justify-content: space-around;
        flex-wrap: wrap;

        image {
          margin-bottom: 10rpx;
        }
      }
    }
  }
  
  // 搜索框样式
  .search-box {
    // 设置定位效果为“吸顶”
    position: sticky;
    // 吸顶的“位置”
    top: 0;
    // 提高层级，防止被轮播图覆盖
    z-index: 999;
  }
</style>